<template>
  <div  class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.name" placeholder="app name搜索" style="width: 200px;" class="filter-item" clearable />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        查找
      </el-button>
      <el-button
        class="filter-item"
        style="margin-left: 10px;"
        type="primary"
        icon="el-icon-edit"
        @click="handleCreate"
      >
        新建app
      </el-button>
    </div>
    <el-table :data="list" border fit highlight-current-row style="width: 100%">
      <el-table-column label="操作" align="center" width="250" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleUpdate(row)">
            编辑
          </el-button>
          <el-button type="primary" size="mini" @click="showRouter(row)">
            查看路由
          </el-button>
          <el-button size="mini" type="danger" @click="handleDelete(row)">
            删除
          </el-button>
        </template>
      </el-table-column>
      <el-table-column align="center" label="ID" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="code">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="中文名称">
        <template slot-scope="scope">
          <span>{{ scope.row.fullname }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="描述">
        <template slot-scope="scope">
          <span>{{ scope.row.description }}</span>
        </template>
      </el-table-column>

    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.pageNumber"
      :limit.sync="listQuery.pageSize"
      @pagination="getList"
    />

    <CreateOrUpdateView
      ref="CreateOrUpdateForm"
      :text-map="textMap"
      :dialog-status="dialogStatus"
      :dialog-form-visible.sync="dialogFormVisible"
      :form-data-parent="formData"
      @on-fresh-list="getList"
    />

    <!-- 路由对话框-->
    <el-dialog :visible.sync="routerViewVisible">
      <MenuTree ref="menuTreeRef" :show-checkbox="false"/>
    </el-dialog>
  </div>
</template>

<script>
  import * as apiApp from '@/api/app'
  import waves from '@/directive/waves' // waves directive
  import Pagination from '@/components/Pagination' // secondary package based on el-pagination
  // 当然你也可以为了方便使用，将它注册到全局
  import permission from '@/directive/permission/index.js' // 权限判断指令
  import CreateOrUpdateView from './createOrUpdateView'
  import MenuTree from '@/views/auth_role/menuTree'
  import { MessageBox } from 'element-ui'

  export default {
    name: 'App',
    components: { Pagination, CreateOrUpdateView, MenuTree },
    directives: { waves, permission },
    data() {
      return {
        list: null,
        listLoading: true,
        total: 0,
        listQuery: { // 查询列表时的参数对象
          pageNumber: 1,
          pageSize: 50,
          name: undefined
        },
        dialogFormVisible: false,
        dialogStatus: '',
        textMap: {
          update: '编辑',
          create: '创建'
        },
        formData: {
          id: undefined,
          name: undefined,
          fullname: undefined,
          description: undefined
        },
        routerViewVisible: false
      }
    },
    created() {
      this.getList()
    },
    methods: {
      async getList() { // 获取列表数据
        this.listLoading = true
        const { content } = await apiApp.getList(this.listQuery)
        this.list = content.list
        this.total = content.totalCounts
        this.listLoading = false
      },
      handleFilter() {
        this.listQuery.pageNumber = 1
        this.getList()
      },
      handleCreate() {
        this.initFormData()
        this.dialogStatus = 'create'
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['CreateOrUpdateForm'].$refs['dataForm'].clearValidate()
        })
      },
      handleUpdate(row) {
        console.log('row--->', row)
        // this.formData = Object.assign({}, row) // copy obj
        // this.$refs['createOrderForm'].initFormData() // 直接调用子组件的method, 很方便
        this.$refs['CreateOrUpdateForm'].formData = Object.assign({}, row)
        this.dialogStatus = 'update'
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['CreateOrUpdateForm'].$refs['dataForm'].clearValidate()
        })
      },
      showRouter(row) {
        console.log('row--->', row)
        const appName = row.name
        this.routerViewVisible = true
        this.$nextTick(() => {
          this.$refs['menuTreeRef'].getAppRouter(appName, false) // 获取某个app的项目路由
        })
      },
      resetFormData() {
        // 清空formData对象
        Object.keys(this.formData).forEach(key => {
          this.formData[key] = undefined
        })
      },
      initFormData() {
        this.resetFormData()
        this.$refs['CreateOrUpdateForm'].initFormData() // 直接调用子组件的method, 很方便
      },
      handleDelete(row) {
        this.$confirm('是否删除?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          const deleteData = Object.assign({}, row) // copy obj
          apiApp.deleteApp(deleteData.id).then(() => {
            // 刷新列表数据
            this.getList()
            this.$notify({
              title: 'Success',
              message: 'Delete Successfully',
              type: 'success',
              duration: 2000
            })
          })
        }).catch(() => {
        })
      }
    }
  }
</script>

<style scoped>

</style>
